package com.zsguan.s.stools4andr.io;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;

/**
 * Created by Administrator on 2015/9/14.
 * File write manager
 */
public class ZFileWriteManager {

    public static void write(String path, String content) throws IOException {
        BufferedWriter bos = null;
        OutputStreamWriter osw = null;
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(path, true);
            osw = new OutputStreamWriter(fos);
            bos = new BufferedWriter(osw);
            bos.write(content);
        } finally {
            if (null != bos) {
                bos.close();
            }
            if (null != osw) {
                osw.close();
            }
            if (null != fos) {
                fos.close();
            }
        }
    }

    public static void clear(String path) throws IOException {
        File file = new File(path);
        FileWriter fileWriter = null;

        try {
            if (file.exists()) {
                fileWriter = new FileWriter(path, false);
                fileWriter.write("");
                fileWriter.close();

                if (file.delete()) {
                    file.deleteOnExit();
                }
            }
        } finally {
            if (null != fileWriter) {
                fileWriter.close();
            }
        }
    }
}
